﻿using MiHoMiao.Mono.UnityPool;
using TMPro;
using TodoList.Mono.MainObject;
using TodoList.TodoTask;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace TodoList.Mono.TodoTask
{
    public partial class TodoTaskItem : MonoBehaviour, IPooledObject, IPointerClickHandler
    {
        private static readonly int m_Unfolded = Animator.StringToHash("Unfolded");
        private static readonly int m_ToBeDelete = Animator.StringToHash("ToBeDelete");
        [SerializeField] private TextMeshProUGUI m_TaskName;
        [SerializeField] private TextMeshProUGUI m_Description;
        [SerializeField] private Image m_BodyImage;
        [SerializeField] private Image m_IsDoneImage;
        [SerializeField] private Image m_IsStaredImage;
        [SerializeField] private Animator m_Animator;
        public Component Source { get; set; }
        
        #region Properties
        
        private int m_Id;
        private TodoTaskTable m_TodoTask;
        public void SetId(int value)
        {
            m_Id = value;
            m_TodoTask = Main.Instance.SqlAccess.QueryAt<TodoTaskTable>(m_Id);
            UpdateData();
            UpdateColor();
        }

        private void UpdateData()
        {
            m_TaskName.text = $"{m_TodoTask.Name}<size=32>--{GetRemainingTime(m_TodoTask.Deadline)}</size>";
            m_Description.text = m_TodoTask.Description;
        }
        
        private void UpdateColor()
        {
            m_TaskName.color = m_TodoTask.IsDone ? new Color(0.5f, 0.5f, 0.5f, 0.5f) : Color.black;
            m_Description.color = m_TodoTask.IsDone ? new Color(0.5f, 0.5f, 0.5f, 0.5f) : Color.black;
            m_IsDoneImage.color = m_TodoTask.IsDone ? Color.black : Color.white;
            m_BodyImage.color = m_TodoTask.IsDone ? new Color(1, 1, 1, 0.25f) : Color.white;
        }

        #endregion

        public void OnPointerClick(PointerEventData eventData) 
            => m_Animator.SetBool(m_Unfolded,!m_Animator.GetBool(m_Unfolded));

    }
}